package com.xqb.modules.controller.course;

import com.xqb.common.annotation.Anonymous;
import com.xqb.common.annotation.Log;
import com.xqb.common.core.controller.BaseController;
import com.xqb.common.core.domain.AjaxResult;
import com.xqb.common.core.page.TableDataInfo;
import com.xqb.common.enums.BusinessType;
import com.xqb.common.utils.poi.ExcelUtil;
import com.xqb.modules.domain.po.InsInstitution;
import com.xqb.modules.domain.po.LesCourse;
import com.xqb.modules.domain.vo.CourseVo;
import com.xqb.modules.domain.vo.InstitutionCourseStatisticsVo;
import com.xqb.modules.service.IInsInstitutionService;
import com.xqb.modules.service.ILesCourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程Controller
 * 
 * @author xqb
 * @date 2024-10-14
 */
@RestController
@RequestMapping("/course/course")
public class LesCourseController extends BaseController
{
    @Autowired
    private ILesCourseService lesCourseService;
    @Autowired
    private IInsInstitutionService insInstitutionService;

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(LesCourse lesCourse)
    {
        startPage();
        List<CourseVo> list = lesCourseService.getLesCourseList(lesCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LesCourse lesCourse)
    {
        List<LesCourse> list = lesCourseService.selectLesCourseList(lesCourse);
        ExcelUtil<LesCourse> util = new ExcelUtil<LesCourse>(LesCourse.class);
        util.exportExcel(response, list, "课程数据");
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(lesCourseService.selectLesCourseById(id));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('course:course:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LesCourse lesCourse)
    {
        InsInstitution insInstitution = insInstitutionService.selectInsInstitutionByName(lesCourse.getInsName());
        Long insId = insInstitution.getId();
        lesCourse.setInsId(insId);
        Long id = lesCourseService.insertLesCourse(lesCourse);
        return success(id);
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('course:course:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LesCourse lesCourse)
    {
        InsInstitution insInstitution = insInstitutionService.selectInsInstitutionByName(lesCourse.getInsName());
        Long insId = insInstitution.getId();
        lesCourse.setInsId(insId);
        Long id = lesCourseService.updateLesCourse(lesCourse);
        return success(id);
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('course:course:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(lesCourseService.deleteLesCourseByIds(ids));
    }

    /**
     * 统计机构课程
     */
    @ApiOperation(value = "统计机构课程")
    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping("/getInsCourseStatistics/{insId}")
    public AjaxResult  getInsCourseStatistics(@PathVariable Long insId) {
        InstitutionCourseStatisticsVo insCourseStatisticsVo = lesCourseService.getInsCourseStatistics(insId);
        return success(insCourseStatisticsVo);
    }

    /**
     * 客户端获取课程详细信息
     */
    @GetMapping(value = "/getInfo/{id}")
    @Anonymous
    public AjaxResult getAllInfo(@PathVariable("id") Long id) {
        logger.info("客户端获取课程详细信息:{}", id);
        return success(lesCourseService.selectLesCourseInfoById(id));
    }

    /**
     * 查询课程基本信息、目录、学习进度
     */
    @ApiOperation("查询课程基本信息、目录、学习进度")
    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping("/{id}/catalogs")
    public AjaxResult queryCourseAndCatalogById(@PathVariable("id") Long courseId) {
        return success(lesCourseService.queryCourseAndCatalogById(courseId));
    }

    /**
     * 客户端获取课程详细信息
     */
    @GetMapping(value = "/getInsInfo/{id}")
    @Anonymous
    public AjaxResult getInsInfo(@PathVariable("id") Long id) {
        logger.info("客户端获取课程详细信息:{}", id);
        return success(lesCourseService.selectLesCourseInfoById(id));
    }

    /**
     * 客户端获取课程详细信息
     */
    @GetMapping(value = "/getInsInfo2/{id}")
    @Anonymous
    public AjaxResult getInsInfo2(@PathVariable("id") Long id) {
        return success(lesCourseService.selectLesCourseInfoById(id));
    }

    /**
     * 用户是否购买了该课程
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/isBuy")
    @Anonymous
    public AjaxResult isBuy(Long courseId, Long userId) {
        return success(lesCourseService.isBuyCourse(courseId, userId));
    }

    /**
     * 收藏或取消收藏课程
     */
    @PreAuthorize("@ss.hasPermi('course:course:edit')")
    @GetMapping(value = "/like/{id}")
    public AjaxResult like(@PathVariable("id") Long id) {
        return success(lesCourseService.updateCourseLike(id));
    }

    /**
     * MySQL全量导入es(测试使用)
     */
    @PostMapping(value = "/importAll")
    public AjaxResult importAll() {
        return success(lesCourseService.importAll());
    }

}
